package com.markedquilt.interceptor;
/*
 * PackageName: com.markedquilt.interceptor
 * ClassName:   RequestInterceptor
 * Author:   zhang
 * Date:     Created in 2020/10/27
 * Description:
 * History:
 * <author>        <time>             <version>          <desc>
 * 作者姓名         修改时间            版本号              描述
 * zhang           2020/10/27      1.0
 */

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Title:
 * Project: ssm
 *
 * @author ZhangKe
 * Created on 2020/10/27
 */
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String name = request.getParameter("name");
        if(name == null || name.equals("")) {
            // 没有登录,重定向到登录页
            System.out.println("未登录，请登录");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
            return false;
        }else {
            System.out.println("已登录，放行请求");
            // 已登录，放行
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("handler处理中，拦截器正在运行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("handler处理完毕，拦截器正在运行");
    }
}
